package com.loopy.servlet.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "RegisterServlet", value = "/person")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String pwdI = request.getParameter("pwdI");
        String sex = request.getParameter("sex");
        String city = request.getParameter("city");
        String introduction = request.getParameter("introduction");
        String[] sports = request.getParameterValues("sport");
        String header = request.getHeader("user-agent");
//        String s = header.split(" ")[3];
        String refStr = "\\((.*)\\)";
        Pattern compile = Pattern.compile(refStr);
        Matcher matcher = compile.matcher(header);
        matcher.find();
        String group = matcher.group(1);
        String[] split = group.split(";");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("用户名称：" + username + "<br>");
        writer.println("用户密码：" + pwd + "<br>");
        writer.println("确认密码：" + pwdI + "<br>");
        String spotrstr = "";
        for (String sport : sports) {
            spotrstr += sport + " ";
        }
        writer.println("喜欢的运动：" + spotrstr + "<br>");
        writer.println("性别：" + sex + "<br>");
        writer.println("喜欢的城市：" + city + "<br>");
        writer.println("自我介绍：" + introduction + "<br>");
//        writer.println(group);
//        writer.println("操作系统"+split[0]);
//        writer.println("操作系统位数"+split[1].trim());
        writer.println(group);
        writer.flush();
        writer.close();

    }
}
